package com.zhaoxi.project.sys.controller;

import com.zhaoxi.common.utils.StringUtils;
import com.zhaoxi.framework.config.ZhaoXiConfig;
import com.zhaoxi.framework.security.LoginBody;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.hrp.service.UserService;
import com.zhaoxi.project.sys.service.impl.SysConfigServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 首页
 *
 * @author zhaoxi
 */
@RestController
public class SysIndexController {
  /**
   * 系统基础配置
   */
  @Autowired
  private ZhaoXiConfig zhaoxiConfig;


  @Autowired
  private UserService userService;

  @Autowired
  private SysConfigServiceImpl configService;

  /**
   * 访问首页，提示语
   */
  @RequestMapping("/")
  public String index() {
    return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", zhaoxiConfig.getName(), zhaoxiConfig.getVersion());
  }

  @PostMapping("/register")
  public PageData register(@RequestBody LoginBody user) {
    if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
      return PageData.error("当前系统没有开启注册功能！");
    }
    return userService.register(user);
  }
}
